<?php
namespace app\exam\controller;
use app\exam\Model\User;
use think\Loader;
use think\Db;
class Register extends Base
{
    //APP注册接口
    public function register(){
        $data=input();
        //验证表单数据是否填写正确
        $validate_result = self::input_validate('Register',$data);
        //如果表单返回为真表示有错误
        if($validate_result){
            return self::res('10001',$validate_result);
        }
        //print_r($data);die;
        $usermodel  = model('user');
        $res=$usermodel->getone(['user_account'=>$data['phone']]);
        //return json_encode($res);die;
//        return self::three()
        if($res){
            return json_encode(['code'=>10001,'msg'=>'已注册了账号']);
        }else{
            $data['user_account']  = $data['phone'];
            $data['header_img']    = $_SERVER ['HTTP_HOST'].Config('iMG_PATH').'avatar.jpg';
            $data['pwd']           = md5(md5($data['pwd'].config('salt').$data['pwd']));
            $data['reg_time']      = time();
            $data['login_flag']    = md5($data['reg_time'].$data['user_account']);
            $b=$usermodel->add($data);
            if($b){
                return self::res('10000',$data['login_flag']);
            }
        }


    }

    //app登陆接口
    public function  login(){
        $data=input();
        $usermodel  = model('user');
        $data['pwd']=md5(md5($data['pwd'].config('salt').$data['pwd']));
        $res=$usermodel->getone(['user_account'=>$data['phone'],'pwd'=>$data['pwd']]);
        return self::three($res,$res['login_flag'],'账号或密码错误');

    }

    public function changePwd(){
        $data=input();
//         $usermodel  = model('user');
         $data['pwd']=md5(md5($data['pwd'].config('salt').$data['pwd']));
         $res=Db::table('user')->where(['login_flag'=>$data['login_flag'],'pwd'=>$data['pwd']])->find();
        if($res){
            if(strlen($data['newPwd'])>=6&&!empty($data['newPwd'])){
                $data['newPwd']=md5(md5($data['newPwd'].config('salt').$data['newPwd']));
                $b=Db::table('user')->where(['login_flag'=>$data['login_flag']])->setField('pwd',$data['newPwd']);
                return self::three($b,'操作成功','操作失败');
            }else{
                return json_encode(['code'=>10001,'msg'=>'新密码不能为空且长度不能少于6']);
            }
        }else{
            return json_encode(['code'=>10001,'msg'=>'密码错误']);
        }
     }

     public function changeImg(){
        $data=input();
        $imgurl=self::upimg();
        if($imgurl){
            $b=Db::table('user')->where(['login_flag'=>$data['login_flag']])->setField('header_img',$imgurl);
             return self::three($b,'修改成功','修改失败');
        }else{
            json(['code'=>10001,'msg'=>'上传出错']);
        }
        // print_r($imgurl);
     }


    public function  getUserinfo(){
        $login_flag=input('login_flag');
        $usermodel  = model('user');
        $res=$usermodel->getone(['login_flag'=>$login_flag]);
        if($res){
            $arr=[];
            $arr['header_img']=$res['header_img'];
            $arr['nickname']=$res['phone'];
            return json(['code'=>10000,'msg'=>'查询成功','data'=>$arr]);
        }else{
            return json(['code'=>10001,'msg'=>'暂无数据']);
        }

    }

    //APP获取短信验证码接口
    public function  getcode(){
        // header("Access-Control-Allow-Origin: *");
        $phone=input('phone');
        // return $phone;die;
        $usermodel  = model('user');
        $res=$usermodel->getone(['user_account'=>$phone]);
        if($res){
            return json_encode(['code'=>10001,'msg'=>'已注册了账号']);
        }else{
             $smscode = rand(0,9).rand(0,9).rand(0,9).rand(0,9);//创建随机数验证码
            $status=send_sms_aldy($phone,$smscode);//发送短信的函数
//        if($status['msg'] == '发送成功'){
//           return json(['code'=>10000,'msg'=>'发送成功','data'=>$smscode]);
//        }else{
//
//           return json(['code'=>10001,'msg'=>'验证码发送频繁,'.$status['msg']]);
//        }
            return self::three($status['msg'] == '发送成功','发送成功','验证码发送频繁,'.$status['msg'],['code'=>$smscode]);
        }
       
    }
    public function  test(){
        echo 'allen';
    }





}

